﻿package 
{
	import flash.events.Event;
	import flash.display.Sprite;

	public class PieChart extends Chart
	{
		var radius:Number;
		
		public function PieChart()
		{
			super();
			radius = 100;
		}
		
		public override function ParseData(inputData:Object)
		{
			super.ParseData(inputData);

			chartAttribute = inputData[name];
			if ('Total' in inputData)
			{
				dataPoints[0] = new Array();
				for (var date:String in inputData['Total'])
				{
					var dataPoint:Number = parseFloat(inputData['Total'][date].replace(/,/g,''));
					dataPoints[0].push(dataPoint);
				}
			} else if ('Subattr' in inputData) {
				var dpIndex:int = 0;
				for (var subattrs:String in inputData['Subattr'])
				{
					if (! 'Total' in inputData['Subattr'][subattrs])
					{
						break;
					}

					dataPoints[dpIndex] = new Array();
					for (var date1:String in inputData['Subattr'][subattrs]['Total'])
					{
						var dataPoint1:Number = parseFloat(inputData['Subattr'][subattrs]['Total'][date1].replace(/,/g,''));
						dataPoints[dpIndex].push(dataPoint1);
					}
					dpIndex++;
				}
			}

			if(dataPoints.length > 1)
			{
				try {
					dataPoints.sort(SortDataPoints);
				} catch (e:Error) {
					DebugCallback(e.name + ': ' + e.message);
				}
			}


			Init(chartx, charty, radius);
		}


		public function Init(cx:Number, cy:Number, r:Number)
		{
			chartx = cx;
			charty = cy;
			radius = r;
			
			var total:Number = 0;
			for(var i:int = 0; i < dataPoints.length; i++)
			{
				for(var j:int = 0; j < dataPoints[i].length; j++)
				{
					total += dataPoints[i][j];
				}
			}
			
			var startAngle:Number = 90;
			graphics.clear();
			
			var dataPointsLength:Number = dataPoints.length;
			if(dataPoints.length == 1)
			{
				dataPointsLength = dataPoints[0].length;
			}
			
			for(var k:int = 0; k < dataPointsLength; k++)
			{
				var dataPoint:Number = 0;
				if(dataPoints.length > 1) {
					for(var l:int = 0; l < dataPoints[k].length; l++)			
					{
						dataPoint += dataPoints[k][l];
					}
				} else {
					dataPoint = dataPoints[0][k];
				}
				
				var arc:Number = dataPoint / total * 360;
	
				graphics.moveTo(chartx, charty);
				arc = Math.min(Math.abs(arc), 360);
			
				var segs:Number = Math.ceil(Math.abs(arc)/45);
				var segAngle:Number = arc/segs;
				var theta:Number = -(segAngle/180)*Math.PI;
				var angle:Number = -(startAngle/180)*Math.PI;
				
				graphics.lineStyle(2, 0x666666);
				graphics.beginFill(colorArray[k], 1);
				if (segs>0) {
					var ax:Number = chartx+Math.cos(startAngle/180*Math.PI)*radius;
					var ay:Number = charty+Math.sin(-startAngle/180*Math.PI)*radius;
					graphics.lineTo(ax, ay);
					for (var m:int = 0; m < segs; m++) {
						angle += theta;
						var angleMid:Number = angle-(theta/2);
						var bx:Number = chartx+Math.cos(angle)*radius;
						var by:Number = charty+Math.sin(angle)*radius;
						var cx:Number = chartx+Math.cos(angleMid)*(radius/Math.cos(theta/2));
						var cy:Number = charty+Math.sin(angleMid)*(radius/Math.cos(theta/2));
						graphics.curveTo(cx, cy, bx, by);
					}
					graphics.lineTo(chartx, charty);
				}
				startAngle += arc;
				graphics.endFill();
			}
		}
	}
}